﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.AP.ARAPCounterApply;
    using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;

	/// <summary>
	/// APCAUnApproveSV partial 
	/// </summary>	
	public partial class APCAUnApproveSV 
	{	
		internal BaseStrategy Select()
		{
			return new APCAUnApproveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class APCAUnApproveSVImpementStrategy : BaseStrategy
	{
		public APCAUnApproveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			APCAUnApproveSV bpObj = (APCAUnApproveSV)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");
				List<DocDTOData> retlist = new List<DocDTOData>();

				foreach (var dto in bpObj.Data)
				{
					ARAPCAHead entity = ARAPCAHead.Finder.Find($"DocNoForUI='{dto.DocNo}'");
					if (entity == null)
					{
						//新增逻辑
						throw new Exception($"单号:{dto.DocNo}不存在!");

					}
					else
					{
						if (entity.DocStatus != UFIDA.U9.CBO.FI.Enums.DirectHandleBillDocStatusEnum.Approved)
							throw new Exception($"单号:{entity.DocNo}不为已审!");

						DocUnApprove(entity);

						DocDTOData retdto = new DocDTOData()
						{
							DocNo = entity.DocNo,
							ID = entity.ID.ToString(),
						};

						retlist.Add(retdto);
					}
				}
				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}

		private void DocUnApprove(ARAPCAHead doc)
		{
			//调用bp
			ARAPCAUnApproveBP bp = new ARAPCAUnApproveBP
			{
				ARAPCAHeadKey = doc.Key,
				CurrentSysVersion = doc.SysVersion,
			};
			bp.Do();
		}
	}

	#endregion
	
	
}